<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="baidu-site-verification" content="5pQM9dB1uO" />
<meta name="google-site-verification" content="dY4Uxh6geXuXnQR_3mASUgoRCnXG4gmKcISzY0_3vNM" />
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">
  <meta http-equiv="Cache-Control" content="no-transform">
  <meta http-equiv="Cache-Control" content="no-siteapp">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"www.chenkl.vip","root":"/","scheme":"Mist","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":"mac"},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":true,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":true,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="本篇文章将会进入一个实战环节：利用之前所涉及到的知识，封装一个拖拽对象。为了能够更好的了解更多的方式与进行对比，下面将会使用两种不同的方式来实现拖拽。">
<meta property="og:type" content="article">
<meta property="og:title" content="前端基础进阶学习(十一)--面向对象实战之封装拖拽对象">
<meta property="og:url" content="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%80)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%AE%9E%E6%88%98%E4%B9%8B%E5%B0%81%E8%A3%85%E6%8B%96%E6%8B%BD%E5%AF%B9%E8%B1%A1/index.html">
<meta property="og:site_name" content="小小前端搬运工">
<meta property="og:description" content="本篇文章将会进入一个实战环节：利用之前所涉及到的知识，封装一个拖拽对象。为了能够更好的了解更多的方式与进行对比，下面将会使用两种不同的方式来实现拖拽。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200816102259.png">
<meta property="article:published_time" content="2020-08-16T09:52:49.000Z">
<meta property="article:modified_time" content="2020-08-22T02:51:25.773Z">
<meta property="article:author" content="Archer">
<meta property="article:tag" content="javaScript">
<meta property="article:tag" content="前端进阶">
<meta property="article:tag" content="面向对象">
<meta property="article:tag" content="拖拽">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200816102259.png">

<link rel="canonical" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%80)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%AE%9E%E6%88%98%E4%B9%8B%E5%B0%81%E8%A3%85%E6%8B%96%E6%8B%BD%E5%AF%B9%E8%B1%A1/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>前端基础进阶学习(十一)--面向对象实战之封装拖拽对象 | 小小前端搬运工</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="小小前端搬运工" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">小小前端搬运工</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <h1 class="site-subtitle" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</h1>
      
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">49</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">7</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">28</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="search-pop-overlay">
  <div class="popup search-popup">
      <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

  </div>
</div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%80)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%AE%9E%E6%88%98%E4%B9%8B%E5%B0%81%E8%A3%85%E6%8B%96%E6%8B%BD%E5%AF%B9%E8%B1%A1/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Archer">
      <meta itemprop="description" content="种一棵树最好的时间是十年前，其次就是现在。">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="小小前端搬运工">
    </span>
      <header class="post-header">
        <h2 class="post-title" itemprop="name headline">
          前端基础进阶学习(十一)--面向对象实战之封装拖拽对象
        </h2>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-08-16 09:52:49" itemprop="dateCreated datePublished" datetime="2020-08-16T09:52:49Z">2020-08-16</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/javaScript/" itemprop="url" rel="index"><span itemprop="name">javaScript</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>本篇文章将会进入一个实战环节：利用之前所涉及到的知识，封装一个拖拽对象。为了能够更好的了解更多的方式与进行对比，下面将会使用两种不同的方式来实现拖拽。</p>
</blockquote>
<a id="more"></a>

<ul>
<li>不封装对象直接实现</li>
<li>利用原生JavaScript封装拖拽对象</li>
</ul>
<h2 id="如何让一个DOM元素动起来"><a href="#如何让一个DOM元素动起来" class="headerlink" title="如何让一个DOM元素动起来"></a>如何让一个DOM元素动起来</h2><blockquote>
<p>我们常常会通过修改元素的<code>top,left,translate</code>来使其的位置发生改变。下面的例子中，每点击一次按钮，对象的元素就会移动5px。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"utf-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;&lt;/title&gt;</span><br><span class="line">    &lt;style <span class="built_in">type</span>=<span class="string">"text/css"</span>&gt;</span><br><span class="line">      div &#123;</span><br><span class="line">        width: 100px;</span><br><span class="line">        height: 100px;</span><br><span class="line">        position: relative;</span><br><span class="line">        left: 0;</span><br><span class="line">        background-color: orange;</span><br><span class="line">        color: <span class="comment">#fff;</span></span><br><span class="line">        text-align: center;</span><br><span class="line">        line-height: 100px;</span><br><span class="line">        margin: 5px;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      button &#123;</span><br><span class="line">        height: 40px;</span><br><span class="line">      &#125;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div class=<span class="string">"elem1"</span>&gt;elem1&lt;/div&gt;</span><br><span class="line">    &lt;div class=<span class="string">"elem2"</span>&gt;elem2&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">    &lt;button class=<span class="string">"moveLeft"</span>&gt;点击修改left移动&lt;/button&gt;</span><br><span class="line">    &lt;button class=<span class="string">"moveTranslate"</span>&gt;修改translate移动&lt;/button&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">  &lt;script <span class="built_in">type</span>=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">    var elem1 = document.querySelector(<span class="string">'.elem1'</span>);</span><br><span class="line">    var elem2 = document.querySelector(<span class="string">'.elem2'</span>);</span><br><span class="line"></span><br><span class="line">    var btn1 = document.querySelector(<span class="string">'.moveLeft'</span>);</span><br><span class="line">    var btn2 = document.querySelector(<span class="string">'.moveTranslate'</span>);</span><br><span class="line"></span><br><span class="line">    var left = 0;</span><br><span class="line">    var translateX = 0;</span><br><span class="line"></span><br><span class="line">    btn1.onclick = <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">      left = left + 5;</span><br><span class="line">      elem1.style.left = left + <span class="string">'px'</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    btn2.onclick = <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">      translateX += 5;</span><br><span class="line">      elem2.style.transform = <span class="string">'translateX('</span> + translateX + <span class="string">'px)'</span>;</span><br><span class="line">    &#125;</span><br><span class="line">  &lt;/script&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<div class="note warning">
            <p>由于修改一个元素的top/left值会引起页面的重绘，而translate不会，因此从性能优化上来判断，我们会优先使用translate属性。</p>
          </div>

<h2 id="如何获取当前浏览器支持的transform兼容写法"><a href="#如何获取当前浏览器支持的transform兼容写法" class="headerlink" title="如何获取当前浏览器支持的transform兼容写法"></a>如何获取当前浏览器支持的transform兼容写法</h2><blockquote>
<p>transform是css3的属性，当我们使用它时就不得不面对兼容性的问题。不同版本浏览器的兼容写法大致有如下几种：<code>[&#39;transform&#39;, &#39;webkitTransform&#39;, &#39;MozTransform&#39;, &#39;msTransform&#39;, &#39;OTransform&#39;]</code></p>
</blockquote>
<blockquote>
<p>因此我们需要判断当前浏览器环境支持的transform属性是哪一种。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">// 获取当前浏览器支持的transform兼容写法</span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">getTransform</span></span>() &#123;</span><br><span class="line">  var transform = <span class="string">''</span>,</span><br><span class="line">    divStyle = document.createElement(<span class="string">'div'</span>).style,</span><br><span class="line">    // 可能涉及到的几种兼容性写法，通过循环找出浏览器识别的那一个</span><br><span class="line">    transformArr = [<span class="string">'transform'</span>, <span class="string">'webkitTransform'</span>, <span class="string">'MozTransform'</span>, <span class="string">'msTransform'</span>, <span class="string">'OTransform'</span>],</span><br><span class="line"></span><br><span class="line">    i = 0,</span><br><span class="line">    len = transformArr.length;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">for</span> (; i &lt; len; i++) &#123;</span><br><span class="line">    <span class="keyword">if</span> (transformArr[i] <span class="keyword">in</span> divStyle) &#123;</span><br><span class="line">      // 找到之后立即返回，结束函数</span><br><span class="line">      <span class="built_in">return</span> transform = transformArr[i];</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  // 如果没有找到，就直接返回空字符串</span><br><span class="line">  <span class="built_in">return</span> transform;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>该方法用于获取浏览器支持的transform属性。如果返回的为空字符串，则表示当前浏览器并不支持transform，这个时候我们就需要使用left，top值来改变元素的位置。如果支持，就改变transform值。</p>
</blockquote>
<h2 id="如何获取元素的初始位置"><a href="#如何获取元素的初始位置" class="headerlink" title="如何获取元素的初始位置"></a>如何获取元素的初始位置</h2><blockquote>
<p>首先需要获取到目标元素的初始位置，因此这里需要一个专门用来获取元素样式的功能函数。但是获取元素样式在IE浏览器与其他浏览器有一些不同，我们需要一个兼容性的写法。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> getStyle(elem, property) &#123;</span><br><span class="line">  // ie通过currentStyle来获取元素的样式，其他浏览器通过getComputedStyle来获取</span><br><span class="line">  <span class="built_in">return</span> document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(elem, <span class="literal">false</span>)[property] : elem.currentStyle[property];</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>有了这个方法之后，就可以开始动手写获取目标元素初始位置的方法了。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> getTargetPos(elem) &#123;</span><br><span class="line">  var pos = &#123; x: 0, y: 0 &#125;</span><br><span class="line">  var transform = getTransform()</span><br><span class="line">  <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">    var transformValue = getStyle(elem, transform)</span><br><span class="line">    <span class="keyword">if</span> (transformValue === <span class="string">'none'</span>) &#123;</span><br><span class="line">      elem.style[transform] = <span class="string">'translate(0, 0)'</span></span><br><span class="line">      <span class="built_in">return</span> pos</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      var temp = transformValue.match(/-?\d+/g)</span><br><span class="line">      <span class="built_in">return</span> pos = &#123;</span><br><span class="line">        x: parseInt(temp[4].trim()),</span><br><span class="line">        y: parseInt(temp[5].trim())</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (getStyle(elem, <span class="string">'position'</span>) === <span class="string">'static'</span>) &#123;</span><br><span class="line">      elem.style.position = <span class="string">'relative'</span></span><br><span class="line">      <span class="built_in">return</span> pos</span><br><span class="line">    &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">      var x = parseInt(getStyle(elem, <span class="string">'left'</span>) ? getStyle(elem, <span class="string">'left'</span>) : 0)</span><br><span class="line">      var y = parseInt(getStyle(elem, <span class="string">'top'</span>) ? getStyle(elem, <span class="string">'top'</span>) : 0)</span><br><span class="line">      <span class="built_in">return</span> pos = &#123;</span><br><span class="line">        x: x,</span><br><span class="line">        y: y</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>在拖拽过程中，我们需要不停的设置目标元素的新位置，这样它才能移动起来，因此我们需要一个设置目标元素位置的方法。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// pos = &#123; x: 200, y: 100 &#125;</span><br><span class="line"><span class="keyword">function</span> setTargetPos(elem, pos) &#123;</span><br><span class="line">  var transform = getTransform()</span><br><span class="line">  <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">    elem.style[transform] = <span class="string">'translate('</span> + pos.x + <span class="string">'px, '</span> + pos.y + <span class="string">'px)'</span></span><br><span class="line">  &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">    elem.style.left = pos.x + <span class="string">'px'</span></span><br><span class="line">    elem.style.top = pos.y + <span class="string">'px'</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="built_in">return</span> elem</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="我们需要用到哪些事件"><a href="#我们需要用到哪些事件" class="headerlink" title="我们需要用到哪些事件"></a>我们需要用到哪些事件</h2><blockquote>
<p>在pc上的浏览器中，结合<code>mousedown、mousemove、mouseup</code>这三个事件可以帮助我们实现拖拽。</p>
</blockquote>
<ul>
<li>mousedown 鼠标按下时触发</li>
<li>mousemove 鼠标按下后拖动时触发</li>
<li>mouseup 鼠标松开时触发</li>
</ul>
<div class="note info">
            <p>而在移动端，分别与之对应的则是touchstart、touchmove、touchend。</p>
          </div>

<blockquote>
<p>当我们将元素绑定这些事件时，有一个事件对象将会作为参数传递给回调函数，通过事件对象，我们可以获取到当前鼠标的精确位置，鼠标位置信息是实现拖拽的关键。</p>
</blockquote>
<h2 id="拖拽的原理"><a href="#拖拽的原理" class="headerlink" title="拖拽的原理"></a>拖拽的原理</h2><blockquote>
<p>当事件触发时，我们可以通过事件对象获取到鼠标的精确位置。这就是实现拖拽的关键。当鼠标按下(mousedown触发)时，我们需要记住鼠标的初始位置与目标元素的初始位置，我们的目标就是实现当鼠标移动时，目标元素也跟着移动，根据厂里我们可以得出如下关系。</p>
</blockquote>
<div class="note info">
            <p>移动后的鼠标位置 - 鼠标初始位置 = 移动后的目标元素位置 - 目标元素的初始位置</p>
          </div>

<blockquote>
<p>如果鼠标位置的差值我们用dis来表示，那么目标元素的位置就等于：</p>
</blockquote>
<div class="note info">
            <p>移动后目标元素的位置 = dis + 目标元素的初始位置</p>
          </div>

<blockquote>
<p>通过事件对象，我们可以精确的知道鼠标的当前位置，因此当鼠标拖动(mousemove)时，我们可以不停的计算出鼠标移动的差值，以此来求出目标元素的当前位置。这个过程，就实现了拖拽。</p>
</blockquote>
<blockquote>
<p>而在鼠标松开(mouseup)结束拖拽时，我们需要处理一些收尾工作。</p>
</blockquote>
<p><img src="https://sanyuanda.oss-cn-hangzhou.aliyuncs.com/imgs/QQ%E5%9B%BE%E7%89%8720200816102259.png" alt=""></p>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// 获取目标元素对象</span><br><span class="line">var oElem = document.getElementById(<span class="string">'target'</span>)</span><br><span class="line"></span><br><span class="line">// 声明2个变量用来保存鼠标初始位置的x，y坐标</span><br><span class="line">var startX = 0</span><br><span class="line">var startY = 0</span><br><span class="line"></span><br><span class="line">// 声明2个变量用来保存目标元素初始位置的x，y坐标</span><br><span class="line">var sourceX = 0</span><br><span class="line">var sourceY = 0</span><br></pre></td></tr></table></figure>

<h3 id="功能函数"><a href="#功能函数" class="headerlink" title="功能函数"></a>功能函数</h3><blockquote>
<p>因为之前已经贴过代码，就不再重复</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">// 获取当前浏览器支持的transform兼容写法</span><br><span class="line"><span class="keyword">function</span> <span class="function"><span class="title">getTransform</span></span>() &#123;&#125;</span><br><span class="line"></span><br><span class="line">// 获取元素属性</span><br><span class="line"><span class="keyword">function</span> getStyle(elem, property) &#123;&#125;</span><br><span class="line"></span><br><span class="line">// 获取元素的初始位置</span><br><span class="line"><span class="keyword">function</span> getTargetPos(elem) &#123;&#125;</span><br><span class="line"></span><br><span class="line">// 设置元素的初始位置</span><br><span class="line"><span class="keyword">function</span> setTargetPos(elem, potions) &#123;&#125;</span><br></pre></td></tr></table></figure>

<h3 id="声明三个事件的回调函数"><a href="#声明三个事件的回调函数" class="headerlink" title="声明三个事件的回调函数"></a>声明三个事件的回调函数</h3><blockquote>
<p>这三个方法就是实现拖拽的核心所在</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><span class="line">// 绑定在mousedown上的回调，event为传入的事件对象</span><br><span class="line"><span class="keyword">function</span> start(event) &#123;</span><br><span class="line">  // 获取鼠标初始位置</span><br><span class="line">  startX = event.pageX</span><br><span class="line">  startY = event.pageY</span><br><span class="line"></span><br><span class="line">  // 获取元素初始位置</span><br><span class="line">  var pos = getTargetPos(oElem)</span><br><span class="line"></span><br><span class="line">  sourceX = pos.x</span><br><span class="line">  sourceY = pos.y</span><br><span class="line"></span><br><span class="line">  // 绑定</span><br><span class="line">  document.addEventListener(<span class="string">'mousemove'</span>, move, <span class="literal">false</span>)</span><br><span class="line">  document.addEventListener(<span class="string">'mouseup'</span>, end, <span class="literal">false</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> move(event) &#123;</span><br><span class="line">  // 获取鼠标当前位置</span><br><span class="line">  var currentX = event.pageX</span><br><span class="line">  var currentY = event.pageY</span><br><span class="line"></span><br><span class="line">  // 计算差值</span><br><span class="line">  var distanceX = currentX - startX</span><br><span class="line">  var distanceY = currentY - startY</span><br><span class="line"></span><br><span class="line">  // 计算并设置元素当前位置</span><br><span class="line">  setTargetPos(oElem, &#123;</span><br><span class="line">    x: (sourceX + distanceX).toFixed(),</span><br><span class="line">    y: (sourceY + distanceY).toFixed()</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">function</span> end(event) &#123;</span><br><span class="line">  document.removeEventListener(<span class="string">'mousemove'</span>, move)</span><br><span class="line">  document.removeEventListener(<span class="string">'mouseup'</span>, end)</span><br><span class="line">  // <span class="keyword">do</span> other things</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<blockquote>
<p>完整拖拽代码如下</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"utf-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;&lt;/title&gt;</span><br><span class="line">    &lt;style <span class="built_in">type</span>=<span class="string">"text/css"</span>&gt;</span><br><span class="line">      <span class="comment">#target &#123;</span></span><br><span class="line">        width: 50px;</span><br><span class="line">        height: 50px;</span><br><span class="line">        background-color: orange;</span><br><span class="line">        cursor: move;</span><br><span class="line">      &#125;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=<span class="string">"target"</span>&gt;&lt;/div&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">  &lt;script <span class="built_in">type</span>=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">    var oElem = document.getElementById(<span class="string">'target'</span>);</span><br><span class="line"></span><br><span class="line">    var startX = 0;</span><br><span class="line">    var startY = 0;</span><br><span class="line"></span><br><span class="line">    var sourceX = 0;</span><br><span class="line">    var sourceY = 0;</span><br><span class="line"></span><br><span class="line">    oElem.addEventListener(<span class="string">'mousedown'</span>, start, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> start(event) &#123;</span><br><span class="line">      startX = event.pageX;</span><br><span class="line">      startY = event.pageY;</span><br><span class="line"></span><br><span class="line">      var pos = getTargetPos(oElem);</span><br><span class="line"></span><br><span class="line">      sourceX = pos.x;</span><br><span class="line">      sourceY = pos.y;</span><br><span class="line"></span><br><span class="line">      document.addEventListener(<span class="string">'mousemove'</span>, move, <span class="literal">false</span>);</span><br><span class="line">      document.addEventListener(<span class="string">'mouseup'</span>, end, <span class="literal">false</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> move(event) &#123;</span><br><span class="line">      var currentX = event.pageX;</span><br><span class="line">      var currentY = event.pageY;</span><br><span class="line"></span><br><span class="line">      var distanceX = currentX - startX;</span><br><span class="line">      var distanceY = currentY - startY;</span><br><span class="line"></span><br><span class="line">      setTargetPos(oElem, &#123;</span><br><span class="line">        x: (sourceX + distanceX).toFixed(),</span><br><span class="line">        y: (sourceY + distanceY).toFixed()</span><br><span class="line">      &#125;)</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> end(event) &#123;</span><br><span class="line">      document.removeEventListener(<span class="string">'mousemove'</span>, move);</span><br><span class="line">      document.removeEventListener(<span class="string">'mouseup'</span>, end);</span><br><span class="line">      // <span class="keyword">do</span> other things</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> getStyle(elem, property) &#123;</span><br><span class="line">      // 低版本ie通过currentStyle来获取元素的样式，其他浏览器通过getComputedStyle来获取</span><br><span class="line">      <span class="built_in">return</span> document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(elem, <span class="literal">false</span>)[property] :</span><br><span class="line">        elem.currentStyle[property];</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> <span class="function"><span class="title">getTransform</span></span>() &#123;</span><br><span class="line">      var transform = <span class="string">''</span>,</span><br><span class="line">        divStyle = document.createElement(<span class="string">'div'</span>).style,</span><br><span class="line">        transformArr = [<span class="string">'transform'</span>, <span class="string">'webkitTransform'</span>, <span class="string">'MozTransform'</span>, <span class="string">'msTransform'</span>, <span class="string">'OTransform'</span>],</span><br><span class="line"></span><br><span class="line">        i = 0,</span><br><span class="line">        len = transformArr.length;</span><br><span class="line"></span><br><span class="line">      <span class="keyword">for</span> (; i &lt; len; i++) &#123;</span><br><span class="line">        <span class="keyword">if</span> (transformArr[i] <span class="keyword">in</span> divStyle) &#123;</span><br><span class="line">          <span class="built_in">return</span> transform = transformArr[i];</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="built_in">return</span> transform;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="keyword">function</span> getTargetPos(elem) &#123;</span><br><span class="line">      var pos = &#123;</span><br><span class="line">        x: 0,</span><br><span class="line">        y: 0</span><br><span class="line">      &#125;;</span><br><span class="line">      var transform = getTransform();</span><br><span class="line">      // transform = <span class="literal">false</span>;</span><br><span class="line">      <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">        var transformValue = getStyle(elem, transform);</span><br><span class="line">        <span class="keyword">if</span> (transformValue == <span class="string">'none'</span>) &#123;</span><br><span class="line">          elem.style[transform] = <span class="string">'translate(0, 0)'</span>;</span><br><span class="line">          <span class="built_in">return</span> pos;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          var temp = transformValue.match(/[0-9,\s\.]+/)[0].split(<span class="string">','</span>);</span><br><span class="line">          <span class="built_in">return</span> pos = &#123;</span><br><span class="line">            x: parseInt(temp[4].trim()),</span><br><span class="line">            y: parseInt(temp[5].trim())</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (getStyle(elem, <span class="string">'position'</span>) == <span class="string">'static'</span>) &#123;</span><br><span class="line">          elem.style.position = <span class="string">'relative'</span>;</span><br><span class="line">          <span class="built_in">return</span> pos;</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">          var x = parseInt(getStyle(elem, <span class="string">'left'</span>) ? getStyle(elem, <span class="string">'left'</span>) : 0);</span><br><span class="line">          var y = parseInt(getStyle(elem, <span class="string">'top'</span>) ? getStyle(elem, <span class="string">'top'</span>) : 0);</span><br><span class="line">          <span class="built_in">return</span> pos = &#123;</span><br><span class="line">            x: x,</span><br><span class="line">            y: y</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    // pos = &#123; x: 200, y: 100 &#125;</span><br><span class="line">    <span class="keyword">function</span> setTargetPos(elem, pos) &#123;</span><br><span class="line">      var transform = getTransform();</span><br><span class="line">      <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">        elem.style[transform] = <span class="string">'translate('</span> + pos.x + <span class="string">'px, '</span> + pos.y + <span class="string">'px)'</span>;</span><br><span class="line">      &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">        elem.style.left = pos.x + <span class="string">'px'</span>;</span><br><span class="line">        elem.style.top = pos.y + <span class="string">'px'</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="built_in">return</span> elem;</span><br><span class="line">    &#125;</span><br><span class="line">  &lt;/script&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>

<h2 id="封装拖拽对象"><a href="#封装拖拽对象" class="headerlink" title="封装拖拽对象"></a>封装拖拽对象</h2><blockquote>
<p>我们的目标是，只要我们声明一个拖拽实例，那么传入的目标元素讲自动具备可以拖拽的功能。</p>
</blockquote>
<blockquote>
<p>在实际开发中，一个对象我们常常会单独放在一个JavaScript文件中，这个JavaScript文件讲单独作为一个模块，利用各种模块的方式组织起来使用。当然这里没有复杂的模块交互，因为这个例子，我们只需要一个模块即可。</p>
</blockquote>
<blockquote>
<p>为了避免变量污染，我们需要将模块放置于一个函数自执行方式模拟的块级作用域中。</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">;(<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">  ...</span><br><span class="line">&#125;)();</span><br></pre></td></tr></table></figure>

<div class="note info">
            <p>在普通的模块组织中，我们只是单纯的将许多JavaScript文件压缩成为一个JavaScript文件，因此此处的第一个分号则是为了防止上一个模块的结尾不用分号导致报错，必不可少。当然在通过require或者ES6模块等方式就不会出现这样的情况。</p>
          </div>

<blockquote>
<p>我们知道，在封装一个对象的时候，可以将属性与方法放置于构造函数或者原型中，而在增加了自执行函数之后，我们又可以将属性与方法放置于模块的内部作用于。这就是闭包的知识。</p>
</blockquote>
<blockquote>
<p><code>那么我们将如何合理的处理属性与方法的位置。</code></p>
</blockquote>
<blockquote>
<p>每一个对象的情况都不一样，不能一概而论，我们需要清洗的知道这三种位置的特性才能做出最合适的决定。</p>
</blockquote>
<ul>
<li>构造函数中：属性与方法为当前实例单独拥有，只能被当前实例访问，并且每声明一个实例，其中的方法都会被重新创建一次。</li>
<li>原型中：属性与方法为所有实例共同拥有，可以被所有实例访问，新声明实例不会重复创建方法。</li>
<li>模块作用域中：属性和方法不能被任何实例访问，但是能被内部方法访问，新声明的实例，不会重复创建相同的方法。</li>
</ul>
<blockquote>
<p><code>对于方法的判断比较简单</code></p>
</blockquote>
<blockquote>
<p>因为在构造函数中的方法总会声明一个新的实例时被重复创建，因此我们声明的方法都尽量避免出现在构造函数中。</p>
</blockquote>
<blockquote>
<p>而如果你的方法中需要用到构造函数中的变量，或者想要公开，那就需要放在原型中。</p>
</blockquote>
<blockquote>
<p>如果方法需要私有不被外界访问，那么就放置在模块作用域中。</p>
</blockquote>
<blockquote>
<p><code>对于属性的判断</code></p>
</blockquote>
<blockquote>
<p>如果属性值只能被实例单独拥有，比如person对象的name，只能属于某一个person实例，又比如这里拖拽对象中，某一个元素的初始位置，也仅仅只是这个元素的当前位置，这个属性，则适合放在构造函数中。</p>
</blockquote>
<blockquote>
<p>而如果一个属性仅仅供内部方法访问，这个属性就适合放在模块作用域中。</p>
</blockquote>
<blockquote>
<p>完整代码如下</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br></pre></td><td class="code"><pre><span class="line">&lt;!DOCTYPE html&gt;</span><br><span class="line">&lt;html&gt;</span><br><span class="line">  &lt;head&gt;</span><br><span class="line">    &lt;meta charset=<span class="string">"utf-8"</span>&gt;</span><br><span class="line">    &lt;title&gt;&lt;/title&gt;</span><br><span class="line">    &lt;style <span class="built_in">type</span>=<span class="string">"text/css"</span>&gt;</span><br><span class="line">      <span class="comment">#target,</span></span><br><span class="line">      <span class="comment">#target2 &#123;</span></span><br><span class="line">        width: 50px;</span><br><span class="line">        height: 50px;</span><br><span class="line">        background-color: orange;</span><br><span class="line">        cursor: move;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="comment">#target2 &#123;</span></span><br><span class="line">        background-color: red;</span><br><span class="line">      &#125;</span><br><span class="line">    &lt;/style&gt;</span><br><span class="line">  &lt;/head&gt;</span><br><span class="line">  &lt;body&gt;</span><br><span class="line">    &lt;div id=<span class="string">"target"</span>&gt;&lt;/div&gt;</span><br><span class="line">    &lt;div id=<span class="string">"target2"</span>&gt;&lt;/div&gt;</span><br><span class="line">  &lt;/body&gt;</span><br><span class="line">  &lt;script <span class="built_in">type</span>=<span class="string">"text/javascript"</span>&gt;</span><br><span class="line">    ;</span><br><span class="line">    (<span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">      // 这是一个私有属性，不需要被实例访问</span><br><span class="line">      var transform = getTransform();</span><br><span class="line"></span><br><span class="line">      <span class="keyword">function</span> Drag(selector) &#123;</span><br><span class="line">        // 放在构造函数中的属性，都是属于每一个实例单独拥有</span><br><span class="line">        this.elem = typeof selector == <span class="string">'Object'</span> ? selector : document.getElementById(selector);</span><br><span class="line">        this.startX = 0;</span><br><span class="line">        this.startY = 0;</span><br><span class="line">        this.sourceX = 0;</span><br><span class="line">        this.sourceY = 0;</span><br><span class="line"></span><br><span class="line">        this.init();</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">      // 原型</span><br><span class="line">      Drag.prototype = &#123;</span><br><span class="line">        constructor: Drag,</span><br><span class="line"></span><br><span class="line">        init: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          // 初始时需要做些什么事情</span><br><span class="line">          this.setDrag();</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 稍作改造，仅用于获取当前元素的属性，类似于getName</span><br><span class="line">        getStyle: <span class="keyword">function</span>(property) &#123;</span><br><span class="line">          <span class="built_in">return</span> document.defaultView.getComputedStyle ? document.defaultView.getComputedStyle(this.elem, <span class="literal">false</span>)[</span><br><span class="line">            property] : this.elem.currentStyle[property];</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 用来获取当前元素的位置信息，注意与之前的不同之处</span><br><span class="line">        getPosition: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          var pos = &#123;</span><br><span class="line">            x: 0,</span><br><span class="line">            y: 0</span><br><span class="line">          &#125;;</span><br><span class="line">          <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">            var transformValue = this.getStyle(transform);</span><br><span class="line">            <span class="keyword">if</span> (transformValue == <span class="string">'none'</span>) &#123;</span><br><span class="line">              this.elem.style[transform] = <span class="string">'translate(0, 0)'</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              var temp = transformValue.match(/-?\d+/g);</span><br><span class="line">              pos = &#123;</span><br><span class="line">                x: parseInt(temp[4].trim()),</span><br><span class="line">                y: parseInt(temp[5].trim())</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (this.getStyle(<span class="string">'position'</span>) == <span class="string">'static'</span>) &#123;</span><br><span class="line">              this.elem.style.position = <span class="string">'relative'</span>;</span><br><span class="line">            &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">              pos = &#123;</span><br><span class="line">                x: parseInt(this.getStyle(<span class="string">'left'</span>) ? this.getStyle(<span class="string">'left'</span>) : 0),</span><br><span class="line">                y: parseInt(this.getStyle(<span class="string">'top'</span>) ? this.getStyle(<span class="string">'top'</span>) : 0)</span><br><span class="line">              &#125;</span><br><span class="line">            &#125;</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="built_in">return</span> pos;</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 用来设置当前元素的位置</span><br><span class="line">        setPostion: <span class="keyword">function</span>(pos) &#123;</span><br><span class="line">          <span class="keyword">if</span> (transform) &#123;</span><br><span class="line">            this.elem.style[transform] = <span class="string">'translate('</span> + pos.x + <span class="string">'px, '</span> + pos.y + <span class="string">'px)'</span>;</span><br><span class="line">          &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            this.elem.style.left = pos.x + <span class="string">'px'</span>;</span><br><span class="line">            this.elem.style.top = pos.y + <span class="string">'px'</span>;</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;,</span><br><span class="line"></span><br><span class="line">        // 该方法用来绑定事件</span><br><span class="line">        setDrag: <span class="function"><span class="title">function</span></span>() &#123;</span><br><span class="line">          var self = this;</span><br><span class="line">          this.elem.addEventListener(<span class="string">'mousedown'</span>, start, <span class="literal">false</span>);</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> start(event) &#123;</span><br><span class="line">            self.startX = event.pageX;</span><br><span class="line">            self.startY = event.pageY;</span><br><span class="line"></span><br><span class="line">            var pos = self.getPosition();</span><br><span class="line"></span><br><span class="line">            self.sourceX = pos.x;</span><br><span class="line">            self.sourceY = pos.y;</span><br><span class="line"></span><br><span class="line">            document.addEventListener(<span class="string">'mousemove'</span>, move, <span class="literal">false</span>);</span><br><span class="line">            document.addEventListener(<span class="string">'mouseup'</span>, end, <span class="literal">false</span>);</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> move(event) &#123;</span><br><span class="line">            var currentX = event.pageX;</span><br><span class="line">            var currentY = event.pageY;</span><br><span class="line"></span><br><span class="line">            var distanceX = currentX - self.startX;</span><br><span class="line">            var distanceY = currentY - self.startY;</span><br><span class="line"></span><br><span class="line">            self.setPostion(&#123;</span><br><span class="line">              x: (self.sourceX + distanceX).toFixed(),</span><br><span class="line">              y: (self.sourceY + distanceY).toFixed()</span><br><span class="line">            &#125;)</span><br><span class="line">          &#125;</span><br><span class="line"></span><br><span class="line">          <span class="keyword">function</span> end(event) &#123;</span><br><span class="line">            document.removeEventListener(<span class="string">'mousemove'</span>, move);</span><br><span class="line">            document.removeEventListener(<span class="string">'mouseup'</span>, end);</span><br><span class="line">            // <span class="keyword">do</span> other things</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      // 私有方法，仅仅用来获取transform的兼容写法</span><br><span class="line">      <span class="keyword">function</span> <span class="function"><span class="title">getTransform</span></span>() &#123;</span><br><span class="line">        var transform = <span class="string">''</span>,</span><br><span class="line">          divStyle = document.createElement(<span class="string">'div'</span>).style,</span><br><span class="line">          transformArr = [<span class="string">'transform'</span>, <span class="string">'webkitTransform'</span>, <span class="string">'MozTransform'</span>, <span class="string">'msTransform'</span>, <span class="string">'OTransform'</span>],</span><br><span class="line"></span><br><span class="line">          i = 0,</span><br><span class="line">          len = transformArr.length;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (; i &lt; len; i++) &#123;</span><br><span class="line">          <span class="keyword">if</span> (transformArr[i] <span class="keyword">in</span> divStyle) &#123;</span><br><span class="line">            <span class="built_in">return</span> transform = transformArr[i];</span><br><span class="line">          &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="built_in">return</span> transform;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      // 一种对外暴露的方式</span><br><span class="line">      window.Drag = Drag;</span><br><span class="line">    &#125;)();</span><br><span class="line"></span><br><span class="line">    // 使用：声明2个拖拽实例</span><br><span class="line">    new Drag(<span class="string">'target'</span>);</span><br><span class="line">    new Drag(<span class="string">'target2'</span>);</span><br><span class="line">  &lt;/script&gt;</span><br><span class="line">&lt;/html&gt;</span><br></pre></td></tr></table></figure>
    </div>

    <div>
      
        <div>
  
    <div style="text-align:center;color: #ccc;font-size:20px;">------------- 本 文 结 束 <i class="fa fa-paw"></i> 感 谢 您 的 阅 读 -------------</div>
  
</div>
      
    </div>
    
    
    
    
        <div class="reward-container">
  <div>只是为了好玩罢了~</div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/images/wechatpay.jpg" alt="Archer 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/images/alipay.jpg" alt="Archer 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>

        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Archer
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://www.chenkl.vip/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%B8%80)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E5%AE%9E%E6%88%98%E4%B9%8B%E5%B0%81%E8%A3%85%E6%8B%96%E6%8B%BD%E5%AF%B9%E8%B1%A1/" title="前端基础进阶学习(十一)--面向对象实战之封装拖拽对象">https://www.chenkl.vip/前端基础进阶学习(十一)--面向对象实战之封装拖拽对象/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" rel="noopener" target="_blank"><i class="fa fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          
          <div class="post-tags">
              <a href="/tags/javaScript/" rel="tag"><i class="fa fa-tag"></i> javaScript</a>
              <a href="/tags/%E5%89%8D%E7%AB%AF%E8%BF%9B%E9%98%B6/" rel="tag"><i class="fa fa-tag"></i> 前端进阶</a>
              <a href="/tags/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1/" rel="tag"><i class="fa fa-tag"></i> 面向对象</a>
              <a href="/tags/%E6%8B%96%E6%8B%BD/" rel="tag"><i class="fa fa-tag"></i> 拖拽</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81)--%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E3%80%81%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E3%80%81%E5%8E%9F%E5%9E%8B%E4%B8%8E%E5%8E%9F%E5%9E%8B%E9%93%BE/" rel="prev" title="前端基础进阶学习(十)--面向对象、构造函数、原型与原型链">
      <i class="fa fa-chevron-left"></i> 前端基础进阶学习(十)--面向对象、构造函数、原型与原型链
    </a></div>
      <div class="post-nav-item">
    <a href="/%E5%89%8D%E7%AB%AF%E5%9F%BA%E7%A1%80%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0(%E5%8D%81%E4%BA%8C)--%E8%AF%A6%E7%BB%86%E5%9B%BE%E8%A7%A3jQuery%E5%AF%B9%E8%B1%A1%EF%BC%8C%E4%BB%A5%E5%8F%8A%E5%A6%82%E4%BD%95%E6%89%A9%E5%B1%95jQuery%E6%8F%92%E4%BB%B6/" rel="next" title="前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件">
      前端基础进阶学习(十二)--详细图解jQuery对象，以及如何扩展jQuery插件 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

  </div>


          </div>
          
    <div class="comments" id="gitalk-container"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#如何让一个DOM元素动起来"><span class="nav-number">1.</span> <span class="nav-text">如何让一个DOM元素动起来</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何获取当前浏览器支持的transform兼容写法"><span class="nav-number">2.</span> <span class="nav-text">如何获取当前浏览器支持的transform兼容写法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#如何获取元素的初始位置"><span class="nav-number">3.</span> <span class="nav-text">如何获取元素的初始位置</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#我们需要用到哪些事件"><span class="nav-number">4.</span> <span class="nav-text">我们需要用到哪些事件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拖拽的原理"><span class="nav-number">5.</span> <span class="nav-text">拖拽的原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#代码实现"><span class="nav-number">6.</span> <span class="nav-text">代码实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#准备工作"><span class="nav-number">6.1.</span> <span class="nav-text">准备工作</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#功能函数"><span class="nav-number">6.2.</span> <span class="nav-text">功能函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#声明三个事件的回调函数"><span class="nav-number">6.3.</span> <span class="nav-text">声明三个事件的回调函数</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#封装拖拽对象"><span class="nav-number">7.</span> <span class="nav-text">封装拖拽对象</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Archer"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Archer</p>
  <div class="site-description" itemprop="description">种一棵树最好的时间是十年前，其次就是现在。</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">28</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">49</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/kailong321200875" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;kailong321200875" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:502431556@qq.com" title="E-Mail → mailto:502431556@qq.com" rel="noopener" target="_blank"><i class="fa fa-fw fa-envelope"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/meng-shou-kong-cheng-43/activities" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;meng-shou-kong-cheng-43&#x2F;activities" rel="noopener" target="_blank"><i class="fa fa-fw fa-ioxhost"></i></a>
      </span>
      <span class="links-of-author-item">
        <a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i></a>
      </span>
  </div>
  <div class="cc-license motion-element" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="/images/cc-by-nc-sa.svg" alt="Creative Commons"></a>
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-fas fa-heartbeat"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Archer</span>
  <span class="post-meta-divider">|</span>
  <span class="post-meta-item-icon">
    <i class="fa fa-area-chart"></i>
  </span>
  <span class="post-count">站点总字数：43.4k字</span>
</div>
  
  
  

        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <span class="post-meta-item" id="busuanzi_container_site_uv" style="display: none;">
      访客数<span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>人次
    </span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item" id="busuanzi_container_site_pv" style="display: none;">
      总访问量<span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>次
    </span>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script>
  <script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var bp = document.createElement('script');
      var curProtocol = window.location.protocol.split(':')[0];
      bp.src = (curProtocol === 'https') ? 'https://zz.bdstatic.com/linksubmit/push.js' : 'http://push.zhanzhang.baidu.com/push.js';
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(bp, s);
    })();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.css">

<script>
NexT.utils.loadComments(document.querySelector('#gitalk-container'), () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js', () => {
    var gitalk = new Gitalk({
      clientID    : 'e6f2f10abe388d591867',
      clientSecret: '580aea9e3da136ac529a71232367d1fdf3591926',
      repo        : 'blogTalk',
      owner       : 'kailong321200875',
      admin       : ['kailong321200875'],
      id          : '156090f822a1739c67f0946bbff52b9f',
        language: 'zh-CN',
      distractionFreeMode: true
    });
    gitalk.render('gitalk-container');
  }, window.Gitalk);
});
</script>

</body>
</html>

<!-- 页面鼠标点击样式 -->

  
    <script src="/js/src/fireworks.js"></script>
  





  <script src="https://cdn.jsdelivr.net/gh/kailong321200875/live2d-widget@1.0.1/autoload.js"></script>

